/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.gwc.extendsplat.sys.web;

import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.UriUtils;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.gwc.extendsplat.sys.entity.SysAttachfile;
import com.gwc.extendsplat.sys.service.SysAttachfileService;

/**
 * 附件管理Controller
 * @author 黎俊涛
 * @version 2017-04-14
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/sysAttachfile")
public class SysAttachfileController extends BaseController {

	@Autowired
	private SysAttachfileService sysAttachfileService;
	
	@ModelAttribute
	public SysAttachfile get(@RequestParam(required=false) String id) {
		SysAttachfile entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysAttachfileService.get(id);
		}
		if (entity == null){
			entity = new SysAttachfile();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:sysAttachfile:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysAttachfile sysAttachfile, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user=UserUtils.getUser();
		sysAttachfile.setUser(user);
		Page<SysAttachfile> page = sysAttachfileService.findPage(new Page<SysAttachfile>(request, response), sysAttachfile); 
		model.addAttribute("page", page);
		return "extendsplat/sys/sysAttachfileList";
	}

	@RequiresPermissions("sys:sysAttachfile:view")
	@RequestMapping(value = "form")
	public String form(SysAttachfile sysAttachfile, Model model) {
		model.addAttribute("sysAttachfile", sysAttachfile);
		return "extendsplat/sys/sysAttachfileForm";
	}

	@RequiresPermissions("sys:sysAttachfile:edit")
	@RequestMapping(value = "save")
	public String save(SysAttachfile sysAttachfile, Model model, RedirectAttributes redirectAttributes) throws UnsupportedEncodingException {

		String basePath=Global.getUserfilesBaseDir()+sysAttachfile.getFilepath().replace("/yueyang", "");
		File file=new File(UriUtils.decode(basePath, "UTF-8"));
		if(file.exists()){
			sysAttachfile.setFilesize(file.length());
		}else{
			return form(sysAttachfile, model);
		}
		if(StringUtils.isEmpty(sysAttachfile.getId()))
			sysAttachfile.setUser(UserUtils.getUser());
		if (!beanValidator(model, sysAttachfile)){
			return form(sysAttachfile, model);
		}
		sysAttachfileService.save(sysAttachfile);
		addMessage(redirectAttributes, "保存附件成功");
		return "redirect:"+Global.getAdminPath()+"/sys/sysAttachfile/?repage";
	}
	
	@RequiresPermissions("sys:sysAttachfile:edit")
	@RequestMapping(value = "delete")
	public String delete(SysAttachfile sysAttachfile, RedirectAttributes redirectAttributes) {
		sysAttachfileService.delete(sysAttachfile);
		addMessage(redirectAttributes, "删除附件成功");
		return "redirect:"+Global.getAdminPath()+"/sys/sysAttachfile/?repage";
	}

}